package com.example.xxtautosignjava.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.xxtautosignjava.common.R;
import com.example.xxtautosignjava.entity.DingshiSchedule;
import com.example.xxtautosignjava.entity.vo.ScheduleVo;
import com.example.xxtautosignjava.service.DingshiScheduleService;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lee
 * @since 2022-10-14
 */
@RestController
@RequestMapping("/xxt/chedule")

public class DingshiScheduleController {

    @Autowired
    DingshiScheduleService service;
    @PostMapping("setSchedule")
    public R setSchedule(@RequestBody ScheduleVo schedule){

        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("phone",schedule.getPhone());
        int count = service.count(wrapper);
        if (count!=0){
            return R.error().message("您已经设置过预签了，请删除");
        }


        System.out.println(schedule);
        Date date =new Date();

        date= new Date(date.getTime()+schedule.getTime()*60*60*1000);

        DingshiSchedule dingshiSchedule = new DingshiSchedule();
        dingshiSchedule.setEndTime(date);
        dingshiSchedule.setPhone(schedule.getPhone());
        dingshiSchedule.setIsDelete(0);
        service.save(dingshiSchedule);

        return R.ok();
    }

    @DeleteMapping("deleteSchedule/{phone}")
    public R deleteSchedule(@PathVariable String phone){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("phone",phone);
        boolean remove = service.remove(wrapper);
        return R.ok().message("删除成功");
    }

    @GetMapping("getSchedule")
    public List<DingshiSchedule> getSchedule(){

        QueryWrapper wrapper =new QueryWrapper();
        Date date = new Date();
        System.out.println(date.getTime());
        long times = date.getTime();//时间戳
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        System.out.println(dateString);
        wrapper.le("end_time", dateString);

        List<DingshiSchedule> list = service.list(wrapper);
        return  list;
    }

    @GetMapping("removeByPhone/{phone}")
    public R removeByphoneGet(@PathVariable String phone){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("phone",phone);

        boolean remove = service.remove(wrapper);
        if (remove){
            return R.ok();
        }
        else {
            return R.error();
        }
    }
    @PostMapping("removeByPhone")
    public R removeByPhone(@RequestBody String phone){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("phone",phone);

        boolean remove = service.remove(wrapper);
        if (remove){
            return R.ok();
        }
        else {
            return R.error();
        }
    }

    @GetMapping("removeByList")

    public Boolean removeByList() throws IOException {

        String url="https://xxt.lovegcu.xyz/monitor/stop";
        List<DingshiSchedule> schedule = getSchedule();
        for (int i = 0; i < schedule.size(); i++) {
            JSONObject jsonObject =new JSONObject();
            jsonObject.put("phone",schedule.get(i).getPhone());

            String s = doPostJson(url, jsonObject.toJSONString());
            System.out.println(s);
//            service.removeById(schedule.get(i).getId());
        }

        return true;
    }
    /**
     * 发送post 请求
     *
     * @param url  请求地址
     * @param json 请求参数
     * @return
     */
    public static String doPostJson(String url, String json) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpClient.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return resultString;
    }

}

